/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */

package com.verisette.ncos.service.card.bizunit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.verisette.ncos.common.ProjectConstant;

public class ReadBGAndExport {
	
//	int width = 260, height = 172;
	//float width = 272.16f, height = 172.80f;
	float width = ((9.2f/2.54f)*72f), height = ((5.7f/2.54f)*72f);
	
    /** The resulting PDF file. */
	public static final String RESULT_DIR = 
		String.format(ProjectConstant.BASE_APP_FOLDER + "\\TEMP\\EXAMPLE\\%s\\", (new SimpleDateFormat("yyyyMMdd'_'HHmmss")).format(new Date()));
    public static final String RESULT
        = String.format(RESULT_DIR + "NameCard_Final.pdf", (new SimpleDateFormat("yyyyMMdd'_'HHmmss")).format(new Date()));
    /** Paths to images. */
    public static final String[] RESOURCES = {
        "bg/BG_Final_printpost.tif"
    };
    
    public static final String[] RESOURCES_IMG = {
    	"pics_match_size.tif"
    };
    
    //size of picture is 0.5167x0.6474 inc.
    
    public static String FONT = ProjectConstant.APP_FONTS_FOLDER + "\\" + ProjectConstant.FONT_PSLPRO_NORMAL;
    public static String FONT_Bold = ProjectConstant.APP_FONTS_FOLDER + "\\" + ProjectConstant.FONT_PSLPRO_BOLD;
    public static String FONT_Italic = ProjectConstant.APP_FONTS_FOLDER + "\\" + ProjectConstant.FONT_PSLPRO_ITALIC;
    public static String AFont = "psl_irene";
    public static String AFont_Italic = "psl_irene_italic";
    
    public static String ENCODE = ProjectConstant.EXPORT_ENCODE_TIS620;

    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
    	Rectangle pagesize = new Rectangle(width, height);
    	
        // step 1
        Document document = new Document(pagesize, 0f, 0f, 0f, 0f);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
//        writer.setPDFXConformance(PdfWriter.PDFX1A2001);
		FontFactory.register(FONT, ProjectConstant.FONT_NAME_PSLPRO_NORMAL);
		FontFactory.register(FONT_Italic, ProjectConstant.FONT_NAME_PSLPRO_ITALIC);
		
        // step 3
        document.open();
        // step 4
        // Adding a series of images
        Image img, pics;
        float factor = 0f;
        
        for (int i = 0; i < RESOURCES.length; i++) {
        	if(writer.getPDFXConformance()==PdfWriter.PDFX1A2001 && RESOURCES[i].indexOf("RGB")>-1) continue;
        	for(String s : RESOURCES_IMG){
        		pics = Image.getInstance(String.format(ProjectConstant.BASE_APP_FOLDER + "\\RESOURCES\\IMAGES\\%s", s));
        		img = Image.getInstance(String.format(ProjectConstant.BASE_APP_FOLDER + "\\RESOURCES\\IMAGES\\%s", RESOURCES[i]));
        		if (img.getScaledWidth() > width || img.getScaledHeight() > height) {
        			img.scaleToFit(width, height);
        		}
        		document.add(img);
        		PdfContentByte over = writer.getDirectContent();
        		float w=80.65f, h=46.62f;
        		float left=198.44f, buttom=99f;
        		pics.scaleToFit(w, h);
        		pics.setAbsolutePosition(left, buttom);
        		pics.setBorder(Image.BOX);
        		pics.setBorderWidth(3);
        		over.saveState();
        		over.addImage(pics);
        		over.restoreState();
        		
        		over.beginText();
        		BaseFont font = BaseFont.createFont(FONT, ENCODE, BaseFont.EMBEDDED);
        		BaseFont fontItalic = BaseFont.createFont(FONT_Italic, ENCODE, BaseFont.EMBEDDED);
        		over.setFontAndSize(font,14);
        		over.showTextAligned(Element.ALIGN_RIGHT, "สมชาย ธรรมเวช", 235, 89, 0);
        		over.setFontAndSize(fontItalic, 11);
        		over.showTextAligned(Element.ALIGN_RIGHT, "หัวหน้าที่ทำการไปรษณีย์หลักสี่", 235, 78, 0);

        		float part2start = 60;
        		over.setFontAndSize(font, 11);
        		over.showTextAligned(Element.ALIGN_RIGHT, "ที่ทำการไปรษณีย์ศูนย์กลางจ่ายพิเศษหลักสี่", 236, part2start-13-factor, 0);
        		over.showTextAligned(Element.ALIGN_RIGHT, "88 หมู่ 3 ถนนแจ้งวัฒนะ แขวงทุ่งสองห้อง เขตหลักสี่ กรุงเทพฯ 10210", 236, part2start-23-factor, 0);
        		over.showTextAligned(Element.ALIGN_RIGHT, "โทรศัพท์ : 0-2573-9915 โทรสาร : 0-2573-7142 ต่อ 12", 236, part2start-33-factor, 0);
        		over.showTextAligned(Element.ALIGN_RIGHT, "e-mail : somchai.th@thailandpost.co.th", 236, part2start-43-factor, 0);
        		
        		over.endText();
        		
        		document.newPage();
        		
        	}
        }
        
        // step 5
        document.close();
    }


    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
    	File dir = new File(RESULT_DIR);
    	if(!dir.exists()) dir.mkdirs();
    	System.out.println("Result folder:" + RESULT_DIR);
//        new ReadBGAndExport().createPdf(ProgramUtils.CUR_PATH+"results/report/namecard_tmp.pdf");
    	new ReadBGAndExport().createPdf(RESULT);
    }
}
